{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### XGBoost 商品分类确定树的最大深度和min_child_weight"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、导入必备工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda2\\envs\\python3\\lib\\site-packages\\sklearn\\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "import seaborn as sns\n",
    "from matplotlib import pyplot\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、读入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 74659 entries, 0 to 74658\n",
      "Columns: 227 entries, bathrooms to work\n",
      "dtypes: float64(9), int64(218)\n",
      "memory usage: 129.3 MB\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 49352 entries, 0 to 49351\n",
      "Columns: 228 entries, bathrooms to interest_level\n",
      "dtypes: float64(9), int64(219)\n",
      "memory usage: 85.8 MB\n",
      "   bathrooms  bedrooms  price  price_bathrooms  price_bedrooms  room_diff  \\\n",
      "0        1.0         1   2950      1475.000000     1475.000000        0.0   \n",
      "1        1.0         2   2850      1425.000000      950.000000       -1.0   \n",
      "2        1.0         1   3758      1879.000000     1879.000000        0.0   \n",
      "3        1.0         2   3300      1650.000000     1100.000000       -1.0   \n",
      "4        2.0         2   4900      1633.333333     1633.333333        0.0   \n",
      "\n",
      "   room_num  Year  Month  Day  ...   virtual  walk  walls  war  washer  water  \\\n",
      "0       2.0  2016      6   11  ...         0     0      0    0       0      0   \n",
      "1       3.0  2016      6   24  ...         0     0      0    1       0      0   \n",
      "2       2.0  2016      6    3  ...         0     0      0    0       0      0   \n",
      "3       3.0  2016      6   11  ...         0     0      0    0       0      0   \n",
      "4       4.0  2016      4   12  ...         0     0      0    1       0      0   \n",
      "\n",
      "   wheelchair  wifi  windows  work  \n",
      "0           0     0        0     0  \n",
      "1           0     0        0     0  \n",
      "2           0     0        0     0  \n",
      "3           1     0        0     0  \n",
      "4           0     0        0     0  \n",
      "\n",
      "[5 rows x 227 columns]\n",
      "data_test  describe:           bathrooms      bedrooms         price  price_bathrooms  \\\n",
      "count  74659.000000  74659.000000  7.465900e+04     74659.000000   \n",
      "mean       1.212915      1.544663  3.749033e+03      1658.561183   \n",
      "std        0.649820      1.107014  9.713092e+03      4771.933806   \n",
      "min        0.000000      0.000000  1.000000e+00         0.500000   \n",
      "25%        1.000000      1.000000  2.495000e+03      1220.000000   \n",
      "50%        1.000000      1.000000  3.150000e+03      1500.000000   \n",
      "75%        1.000000      2.000000  4.100000e+03      1850.000000   \n",
      "max      112.000000      7.000000  1.675000e+06    837500.000000   \n",
      "\n",
      "       price_bedrooms     room_diff      room_num     Year         Month  \\\n",
      "count    74659.000000  74659.000000  74659.000000  74659.0  74659.000000   \n",
      "mean      1631.330597     -0.331748      2.757578   2016.0      5.015738   \n",
      "std       4482.208640      1.026154      1.497497      0.0      0.825815   \n",
      "min          0.333333     -6.000000      0.000000   2016.0      4.000000   \n",
      "25%       1065.000000     -1.000000      2.000000   2016.0      4.000000   \n",
      "50%       1377.500000      0.000000      2.000000   2016.0      5.000000   \n",
      "75%       1950.000000      0.000000      4.000000   2016.0      6.000000   \n",
      "max     558333.333333    109.000000    115.000000   2016.0      6.000000   \n",
      "\n",
      "                Day      ...            virtual          walk         walls  \\\n",
      "count  74659.000000      ...       74659.000000  74659.000000  74659.000000   \n",
      "mean      15.151623      ...           0.001058      0.003094      0.000442   \n",
      "std        8.245418      ...           0.032922      0.055539      0.021020   \n",
      "min        1.000000      ...           0.000000      0.000000      0.000000   \n",
      "25%        8.000000      ...           0.000000      0.000000      0.000000   \n",
      "50%       15.000000      ...           0.000000      0.000000      0.000000   \n",
      "75%       22.000000      ...           0.000000      0.000000      0.000000   \n",
      "max       31.000000      ...           2.000000      1.000000      1.000000   \n",
      "\n",
      "                war        washer         water    wheelchair          wifi  \\\n",
      "count  74659.000000  74659.000000  74659.000000  74659.000000  74659.000000   \n",
      "mean       0.188243      0.008653      0.000388      0.027994      0.002156   \n",
      "std        0.390908      0.097548      0.019705      0.164957      0.046388   \n",
      "min        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "25%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "50%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "75%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "max        1.000000      2.000000      1.000000      1.000000      1.000000   \n",
      "\n",
      "            windows          work  \n",
      "count  74659.000000  74659.000000  \n",
      "mean       0.001085      0.000884  \n",
      "std        0.032921      0.029720  \n",
      "min        0.000000      0.000000  \n",
      "25%        0.000000      0.000000  \n",
      "50%        0.000000      0.000000  \n",
      "75%        0.000000      0.000000  \n",
      "max        1.000000      1.000000  \n",
      "\n",
      "[8 rows x 227 columns]\n",
      "data_train  describe:          bathrooms      bedrooms         price  price_bathrooms  \\\n",
      "count  49352.00000  49352.000000  4.935200e+04     4.935200e+04   \n",
      "mean       1.21218      1.541640  3.830174e+03     1.697863e+03   \n",
      "std        0.50142      1.115018  2.206687e+04     1.100477e+04   \n",
      "min        0.00000      0.000000  4.300000e+01     2.150000e+01   \n",
      "25%        1.00000      1.000000  2.500000e+03     1.225000e+03   \n",
      "50%        1.00000      1.000000  3.150000e+03     1.500000e+03   \n",
      "75%        1.00000      2.000000  4.100000e+03     1.850000e+03   \n",
      "max       10.00000      8.000000  4.490000e+06     2.245000e+06   \n",
      "\n",
      "       price_bedrooms     room_diff      room_num     Year         Month  \\\n",
      "count    4.935200e+04  49352.000000  49352.000000  49352.0  49352.000000   \n",
      "mean     1.657567e+03     -0.329460      2.753820   2016.0      5.014852   \n",
      "std      7.817996e+03      0.947732      1.446091      0.0      0.824442   \n",
      "min      4.300000e+01     -5.000000      0.000000   2016.0      4.000000   \n",
      "25%      1.066667e+03     -1.000000      2.000000   2016.0      4.000000   \n",
      "50%      1.383417e+03      0.000000      2.000000   2016.0      5.000000   \n",
      "75%      1.962500e+03      0.000000      4.000000   2016.0      6.000000   \n",
      "max      1.496667e+06      8.000000     13.500000   2016.0      6.000000   \n",
      "\n",
      "                Day       ...                walk         walls           war  \\\n",
      "count  49352.000000       ...        49352.000000  49352.000000  49352.000000   \n",
      "mean      15.206881       ...            0.003080      0.000385      0.186477   \n",
      "std        8.280749       ...            0.055412      0.019618      0.389495   \n",
      "min        1.000000       ...            0.000000      0.000000      0.000000   \n",
      "25%        8.000000       ...            0.000000      0.000000      0.000000   \n",
      "50%       15.000000       ...            0.000000      0.000000      0.000000   \n",
      "75%       22.000000       ...            0.000000      0.000000      0.000000   \n",
      "max       31.000000       ...            1.000000      1.000000      1.000000   \n",
      "\n",
      "             washer         water    wheelchair          wifi       windows  \\\n",
      "count  49352.000000  49352.000000  49352.000000  49352.000000  49352.000000   \n",
      "mean       0.009361      0.000446      0.028165      0.002026      0.001013   \n",
      "std        0.101625      0.021109      0.165446      0.044969      0.031814   \n",
      "min        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "25%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "50%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "75%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "max        2.000000      1.000000      1.000000      1.000000      1.000000   \n",
      "\n",
      "               work  interest_level  \n",
      "count  49352.000000    49352.000000  \n",
      "mean       0.000952        1.616895  \n",
      "std        0.030846        0.626035  \n",
      "min        0.000000        0.000000  \n",
      "25%        0.000000        1.000000  \n",
      "50%        0.000000        2.000000  \n",
      "75%        0.000000        2.000000  \n",
      "max        1.000000        2.000000  \n",
      "\n",
      "[8 rows x 228 columns]\n"
     ]
    }
   ],
   "source": [
    "data_test = pd.read_csv('./data/'+\"RentListingInquries_FE_test.csv\")\n",
    "data_train = pd.read_csv('./data/'+\"RentListingInquries_FE_train.csv\")\n",
    "data_test.info()\n",
    "data_train.info()\n",
    "print(data_test.head(5))\n",
    "data_train.head(5)\n",
    "\n",
    "print(\"data_test  describe:\",data_test.describe())\n",
    "print(\"data_train  describe:\",data_train.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAf0AAAFXCAYAAACoS5cAAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X90VOWB//HPZGYSZWayBIyeUhl/ICkLFgxEursMuKCc\nULe02AV04sZdcQVTA4QDaRCjkQaQlAZ2ocbfrZBuEqNsEcvuti6FpDE5yGYNKWDcLmsFlCOBRcgM\nJCHJ/f7Rr7Nm1zACuROS5/06x3OcJ8+d+TycnPOZ5+bOHYdlWZYAAMCAF9fXAQAAQGxQ+gAAGILS\nBwDAEJQ+AACGoPQBADAEpQ8AgCFcfR3Abs3NLX0dAQCAmEpO9n3hODt9AAAMQekDAGAISh8AAENQ\n+gAAGMK2C/k6OzuVn5+vDz74QA6HQytXrlRHR4cWLFigG2+8UZIUDAZ19913q7KyUhUVFXK5XMrK\nytLUqVPV2tqq3NxcnTx5Uh6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bS06Hvf\n+56tr/HYY4/po48+svU1ANNR+gCiOn36tJqammx9jT179ohPEAP24nP6AKJ65JFHVFNTozvuuEO3\n3HKL6urqdPr0aSUlJWnTpk1KTk7Wn/zJn2jMmDE6ceKEXn/9dW3cuFG//OUvlZSUpOTkZE2bNk3f\n/e53tW3bNm3evFldXV0aM2aMCgoKtHnzZm3cuFF+v1//8A//oKSkpL5eMjAgsdMHEFV+fr6uvfZa\nff/739d//dd/qaKiQr/85S/l9/v15ptvSpJOnTql+fPn64033tBvfvMb1dfX6xe/+IVeeOEFHTx4\nUJL0u9/9TpWVlaqoqNAbb7yhoUOH6uWXX9b8+fN17bXX6oUXXqDwARtxG14AX9oNN9ygvLw8vfba\na/rggw/U0NAgv98f+fm4ceMkSbW1tfrmN7+p+Ph4xcfH66677pL0h1P4H374oebOnStJOn/+vEaP\nHh37hQCGovQBfGn79+/X0qVL9Td/8zdKT09XXFxct7/Df3Yv9Li4OHV1df2f4zs7O/XNb35T+fn5\nkqRwOKzOzs7YhAfA6X0A0blcLnV0dGjv3r2aOHGigsGgbrnlFr399ttfWNqTJk3Sr371K7W3tysU\nCmn37t1yOBz6xje+obfeeksnT56UZVl66qmntHnzZkmS0+nkDQBgM3b6AKIaOnSohg0bpl//+tdq\nbW3VzJkz5Xa79bWvfU1Hjx79P/PvuOMO/fu//7vuuece/dEf/ZGuvfZaJSQkaNSoUcrOztZf//Vf\nq6urS3/8x3+s+fPnS5L+/M//XPPnz9dLL72k4cOHx3qJgBG4eh9Ar3v33Xf1+9//Xvfcc4/Onz+v\ne++9V2vWrNGoUaP6OhpgNEofQK/79NNPtXTpUjU3N8uyLM2aNUsPPfRQX8cCjEfpAwBgCC7kAwDA\nEJQ+AACGoPQBADAEpQ8AgCEofQAADEHpAwBgiP8H5R+WUTMNDlkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2c42b7becf8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y_train = data_train['interest_level']\n",
    "x_train = data_train.drop('interest_level',axis = 1)\n",
    "sns.countplot(y_train)\n",
    "pyplot.xlabel('target')\n",
    "pyplot.ylabel('number of target')\n",
    "pyplot.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、通过GridSearchCV  进行树的最大深度和min_child_weight  调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gridsearchcv  获取值： [mean: -0.59789, std: 0.00313, params: {'max_depth': 3, 'min_child_weight': 1}, mean: -0.59766, std: 0.00338, params: {'max_depth': 3, 'min_child_weight': 3}, mean: -0.59756, std: 0.00330, params: {'max_depth': 3, 'min_child_weight': 5}, mean: -0.58840, std: 0.00368, params: {'max_depth': 5, 'min_child_weight': 1}, mean: -0.58844, std: 0.00377, params: {'max_depth': 5, 'min_child_weight': 3}, mean: -0.58823, std: 0.00316, params: {'max_depth': 5, 'min_child_weight': 5}, mean: -0.59232, std: 0.00478, params: {'max_depth': 7, 'min_child_weight': 1}, mean: -0.59099, std: 0.00459, params: {'max_depth': 7, 'min_child_weight': 3}, mean: -0.59032, std: 0.00401, params: {'max_depth': 7, 'min_child_weight': 5}, mean: -0.61121, std: 0.00612, params: {'max_depth': 9, 'min_child_weight': 1}, mean: -0.60361, std: 0.00429, params: {'max_depth': 9, 'min_child_weight': 3}, mean: -0.60033, std: 0.00393, params: {'max_depth': 9, 'min_child_weight': 5}]\n",
      "gridsearchcv  最佳参数： {'max_depth': 5, 'min_child_weight': 5}\n",
      "gridsearchcv 最佳得分： -0.588234021962\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda2\\envs\\python3\\lib\\site-packages\\sklearn\\model_selection\\_search.py:747: DeprecationWarning: The grid_scores_ attribute was deprecated in version 0.18 in favor of the more elaborate cv_results_ attribute. The grid_scores_ attribute will not be available from 0.20\n",
      "  DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "kfold = StratifiedKFold(n_splits = 5, shuffle=True, random_state=3)\n",
    "\n",
    "max_depth = range(3,10,2)\n",
    "min_child_weight = range(1,6,2)\n",
    "param_test = dict(max_depth=max_depth, min_child_weight=min_child_weight)\n",
    "\n",
    "xgb2 = XGBClassifier(\n",
    "    learning_rate=0.1,\n",
    "    n_estimators=281, #通过xgboost  cv方法得出结论\n",
    "    max_depth=5,\n",
    "    min_child_weight=1,\n",
    "    gamma=0,\n",
    "    subsample=0.3,\n",
    "    colsample_bytree=0.8,\n",
    "    colsample_bylevel=0.7,\n",
    "    objective='multi:softprob',\n",
    "    seed=3\n",
    ")\n",
    "\n",
    "gridSearchCV=GridSearchCV(xgb2, param_grid = param_test, scoring='neg_log_loss', n_jobs=-1, cv=kfold)\n",
    "gridSearchCV.fit(x_train,y_train)\n",
    "\n",
    "print(\"gridsearchcv  获取值：\", gridSearchCV.grid_scores_)\n",
    "print(\"gridsearchcv  最佳参数：\", gridSearchCV.best_params_)\n",
    "print(\"gridsearchcv 最佳得分：\", gridSearchCV.best_score_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 通过GridSearchCV 得出树的最大深度为5， min_child_weight也为5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
